﻿#include <QMessageBox>
#include <QFile>
#include "CRobTaskOper.h"
#include "CHeader.h"
#include "CSqlOper.h"
#include "CTaskRuleDlg.h"
#include "CLogOper.h"

CRobTaskOper::CRobTaskOper(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	m_nType = 0;
	m_sGuid = "";
	m_sTaskGuid = "";
	setStyleSheetFile();
	initConnect();
}

CRobTaskOper::~CRobTaskOper()
{}

void CRobTaskOper::setData(QMap<int, QString> mapTaskType, QMap<int, QString> mapPeriodType, QMap<QString, QString> mapTemplate)
{
	m_mapPeriodType = mapPeriodType;
	m_mapTaskType = mapTaskType;
	m_mapTemplate = mapTemplate;
	ui.dateTimeEdit->setDateTime(QDateTime::currentDateTime());
	m_mapPeriodType.remove(-1);
	m_mapTaskType.remove(-1);
	m_mapTemplate.remove("");
	for (QMap<int, QString>::iterator itor = m_mapTaskType.begin(); itor != m_mapTaskType.end(); itor++)
	{
		QString sValue = itor.value();
		if(sValue.contains(A2T("立即执行")))
		{
			continue;
		}
		ui.comboBoxTaskType->addItem(sValue);
	}

	for (QMap<int, QString>::iterator itor = m_mapPeriodType.begin(); itor != m_mapPeriodType.end(); itor++)
	{
		QString sValue = itor.value();
		ui.comboBoxPeriod->addItem(sValue);
	}

	for (QMap<QString, QString>::iterator itor = m_mapTemplate.begin(); itor != m_mapTemplate.end(); itor++)
	{
		QString sValue = itor.value();
		ui.comboBoxTemplate->addItem(sValue);
	}
}

void CRobTaskOper::setTitle(QString sTitle)
{
	this->setWindowTitle(sTitle);
}

void CRobTaskOper::setUI(QString sTaskName, QString sTemplateName, QString sTime, int nTaskType, int nPeriodType, int nContIrTemp, int nContSoundDet)
{
	ui.comboBoxTemplate->setCurrentText(sTemplateName);
	ui.dateTimeEdit->setDateTime(QDateTime::fromString(sTime, "hh:mm:ss"));
	ui.comboBoxTaskType->setCurrentText(m_mapTaskType.value(nTaskType));
	ui.comboBoxPeriod->setCurrentText(m_mapPeriodType.value(nPeriodType));
	//wxy++20240430
	ui.checkBoxContIrTemp->setChecked(nContIrTemp == 0 ? false : true);
	ui.checkBoxContSoundDet->setChecked(nContSoundDet == 0 ? false : true);
	ui.lineEditTaskName->setText(sTaskName);
}

void CRobTaskOper::setStyleSheetFile()
{
	//设置对话框标题
	this->setWindowTitle(A2T("任务配置"));
	QString sQss = setUiStyle("page");
	QString sMyqss = styleSheet();
	QString sMerged = sQss + "\n" + sMyqss; // 后面的会覆盖前面的相同规则	
	setStyleSheet(sMerged);
}

void CRobTaskOper::initConnect()
{
	connect(ui.pushButtonSave, &QPushButton::clicked, this, &CRobTaskOper::slotPushButtonSaveClicked);
	connect(ui.pushButtonCancle, &QPushButton::clicked, this, &CRobTaskOper::slotPushButtonCancleClicked);
	connect(ui.comboBoxTaskType, &QComboBox::currentTextChanged, this, &CRobTaskOper::slotComboxTaskTypeCurrentTextChanged);
}

bool CRobTaskOper::isTaskRuleOk(int nType,QString sNewGuid)
{
	QVector<int> vecTimeType;
	//周期类型(0:12h 1:6h 2:4h 3:2h 4:1h 5:0.5h)
	if (nType == 0)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(24);//12:00
	}
	else if (nType == 1)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(12);//06:00
		vecTimeType.push_back(24);//12:00
		vecTimeType.push_back(36);//18:00
	}
	else if (nType == 2)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(8);//04:00
		vecTimeType.push_back(16);//08:00
		vecTimeType.push_back(24);//12:00
		vecTimeType.push_back(32);//16:00
		vecTimeType.push_back(40);//20:00
	}
	else if (nType == 3)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(4);//02:00
		vecTimeType.push_back(8);//04:00
		vecTimeType.push_back(12);//06:00
		vecTimeType.push_back(16);//08:00
		vecTimeType.push_back(20);//10:00
		vecTimeType.push_back(24);//12:00
		vecTimeType.push_back(28);//14:00
		vecTimeType.push_back(32);//16:00
		vecTimeType.push_back(36);//18:00
		vecTimeType.push_back(40);//20:00
		vecTimeType.push_back(44);//22:00
	}
	else if (nType == 4)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(2);//01:00
		vecTimeType.push_back(4);//02:00
		vecTimeType.push_back(6);//03:00
		vecTimeType.push_back(8);//04:00
		vecTimeType.push_back(10);//05:00
		vecTimeType.push_back(12);//06:00
		vecTimeType.push_back(14);//07:00
		vecTimeType.push_back(16);//08:00
		vecTimeType.push_back(18);//09:00
		vecTimeType.push_back(20);//10:00
		vecTimeType.push_back(22);//11:00
		vecTimeType.push_back(24);//12:00
		vecTimeType.push_back(26);//13:00
		vecTimeType.push_back(28);//14:00
		vecTimeType.push_back(30);//15:00
		vecTimeType.push_back(32);//16:00
		vecTimeType.push_back(34);//17:00
		vecTimeType.push_back(36);//18:00
		vecTimeType.push_back(38);//19:00
		vecTimeType.push_back(40);//20:00
		vecTimeType.push_back(42);//21:00
		vecTimeType.push_back(44);//22:00
		vecTimeType.push_back(46);//23:00
	}
	else if (nType == 5)
	{
		vecTimeType.push_back(0);//00:00
		vecTimeType.push_back(1);//00:30
		vecTimeType.push_back(2);//01:00
		vecTimeType.push_back(3);//01:30
		vecTimeType.push_back(4);//02:00
		vecTimeType.push_back(5);//02:30
		vecTimeType.push_back(6);//03:00
		vecTimeType.push_back(7);//03:30
		vecTimeType.push_back(8);//04:00
		vecTimeType.push_back(9);//04:30
		vecTimeType.push_back(10);//05:00
		vecTimeType.push_back(11);//05:30
		vecTimeType.push_back(12);//06:00
		vecTimeType.push_back(13);//06:30
		vecTimeType.push_back(14);//07:00
		vecTimeType.push_back(15);//07:30
		vecTimeType.push_back(16);//08:00
		vecTimeType.push_back(17);//08:30
		vecTimeType.push_back(18);//09:00
		vecTimeType.push_back(19);//09:30
		vecTimeType.push_back(20);//10:00
		vecTimeType.push_back(21);//10:30
		vecTimeType.push_back(22);//11:00
		vecTimeType.push_back(23);//11:30
		vecTimeType.push_back(24);//12:00
		vecTimeType.push_back(25);//12:30
		vecTimeType.push_back(26);//13:00
		vecTimeType.push_back(27);//13:30
		vecTimeType.push_back(28);//14:00
		vecTimeType.push_back(29);//14:30
		vecTimeType.push_back(30);//15:00
		vecTimeType.push_back(31);//15:30
		vecTimeType.push_back(32);//16:00
		vecTimeType.push_back(33);//16:30
		vecTimeType.push_back(34);//17:00
		vecTimeType.push_back(35);//17:30
		vecTimeType.push_back(36);//18:00
		vecTimeType.push_back(37);//18:30
		vecTimeType.push_back(38);//19:00
		vecTimeType.push_back(39);//19:30
		vecTimeType.push_back(40);//20:00
		vecTimeType.push_back(41);//20:30
		vecTimeType.push_back(42);//21:00
		vecTimeType.push_back(43);//21:30
		vecTimeType.push_back(44);//22:00
		vecTimeType.push_back(45);//22:30
		vecTimeType.push_back(46);//23:00
		vecTimeType.push_back(47);//23:30
	}

	//查询数据
	QString sErrMsg = "";
	QVector<QSqlRecord> vecRecord;
	bool bRet = selectTaskRule(m_sGuid, vecRecord, sErrMsg);
	if (bRet == false)
	{
		SExecLog log;
		log.m_nAlarmLevel = enSysAlarm;
		log.m_sFuncName = A2T("数据库");
		log.m_sDescriptionl = sErrMsg;
		CLogOper::instance().addMsg(log, m_sGuid);
	}
	QMap<int, QString> mapTaskRule;
	for (int i = 0; i < vecRecord.size(); i++)
	{
		mapTaskRule.insert(vecRecord[i].value("TIME_TYPE").toInt(), vecRecord[i].value("TASK_GUID").toString());
	}
	int nCount = 0;//发生冲突的个数
	for (int i = 0; i < vecTimeType.size(); i++)
	{
		if (mapTaskRule[vecTimeType[i]] != "")//之前的座位已经有值
		{
			if (mapTaskRule[vecTimeType[i]] != sNewGuid)
			{
				nCount++;
			}
		}
	}
	if (nCount == 0)//没有冲突  顺便将座位填好
	{
		updateTaskRule(m_sGuid, vecTimeType,sNewGuid, sErrMsg);
		return true;
	}
	else
	{
		return false;
	}
}

void CRobTaskOper::slotPushButtonSaveClicked()
{
	//合法性校验
	if (ui.lineEditTaskName->text() == "")
	{
		messageBox(0, A2T("请将数据填写完整后再进行操作"));
		return;
	}
	if (m_nType == 0)//添加
	{
		QString sGuid = createGuid();
		//周期性任务首先判断是否存在任务冲突
		if (ui.comboBoxTaskType->currentText() == A2T("周期性任务"))
		{
			bool bRet = isTaskRuleOk(m_mapPeriodType.key(ui.comboBoxPeriod->currentText()),sGuid);
			if (bRet == false)
			{
				CTaskRuleDlg oper(m_sGuid, sGuid, ui.lineEditTaskName->text(), m_mapPeriodType.key(ui.comboBoxPeriod->currentText()));
				int nResult = oper.exec();
				if (nResult == QDialog::Rejected)
				{
					messageBox(0, A2T("请配置周期任务执行规则后再进行保存"));
					return;
				}
			}
		}
		CTaskInfo taskInfo;
		taskInfo.m_sTaskGuid = sGuid;
		taskInfo.m_nLatestRecord = 1;
		taskInfo.m_nTaskType = m_mapTaskType.key(ui.comboBoxTaskType->currentText());
		if (ui.comboBoxTaskType->currentText() == A2T("周期性任务"))
		{
			taskInfo.m_nPeriodType = m_mapPeriodType.key(ui.comboBoxPeriod->currentText());
			taskInfo.m_sScheduledTime = ui.dateTimeEdit->dateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("定时性任务"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = ui.dateTimeEdit->dateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("立即执行任务"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("信号触发任务A侧"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("信号触发任务B侧"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		taskInfo.m_sGuid = m_sGuid;
		taskInfo.m_sTaskName = ui.lineEditTaskName->text();
		taskInfo.m_sTemplateFixedID = m_mapTemplate.key(ui.comboBoxTemplate->currentText());

		//wxy++20240430
		taskInfo.m_nContIrTemp = ui.checkBoxContIrTemp->isChecked() ? 1 : 0;//是否开启连续红外测温(0:关闭 1:打开)
		taskInfo.m_nContSoundDet = ui.checkBoxContSoundDet->isChecked() ? 1 : 0;//是否开启连续噪声检测(0:关闭 1:打开)

		QString sErrMsg = "";
		addTaskInfo(taskInfo, sErrMsg);

		SExecLog sLog;
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = ui.lineEditTaskName->text();
		/*sLog.m_sDescriptionl = A2T("%1添加了%2名为%3的任务，其中模版名:%4,任务类型:%5,执行周期:%6")
			.arg(CPublicData::instance().m_userInfo.sUserName)
			.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
			.arg(ui.lineEditTaskName->text())
			.arg(ui.comboBoxTemplate->currentText())
			.arg(ui.comboBoxTaskType->currentText())
			.arg(ui.comboBoxPeriod->currentText());*/
		sLog.m_sFuncName = A2T("任务");
		CLogOper::instance().addMsg(sLog, m_sGuid);
	}
	else if (m_nType == 1)//修改
	{
		//周期性任务首先判断是否存在任务冲突
		if (ui.comboBoxTaskType->currentText() == A2T("周期性任务"))
		{
			bool bRet = isTaskRuleOk(m_mapPeriodType.key(ui.comboBoxPeriod->currentText()), m_sTaskGuid);
			if (bRet == false)
			{
				CTaskRuleDlg oper(m_sGuid, m_sTaskGuid, ui.lineEditTaskName->text(), m_mapPeriodType.key(ui.comboBoxPeriod->currentText()));
				int nResult = oper.exec();
				if (nResult == QDialog::Rejected)
				{
					messageBox(0, A2T("请配置周期任务执行规则后再进行保存"));
					return;
				}
			}
		}
		CTaskInfo taskInfo;
		taskInfo.m_sTaskGuid = m_sTaskGuid;
		taskInfo.m_nLatestRecord = 1;
		taskInfo.m_nTaskType = m_mapTaskType.key(ui.comboBoxTaskType->currentText());
		if (ui.comboBoxTaskType->currentText() == A2T("周期性任务"))
		{
			taskInfo.m_nPeriodType = m_mapPeriodType.key(ui.comboBoxPeriod->currentText());
			taskInfo.m_sScheduledTime = ui.dateTimeEdit->dateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("定时性任务"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = ui.dateTimeEdit->dateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("立即执行任务"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("信号触发任务A侧"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		else if (ui.comboBoxTaskType->currentText() == A2T("信号触发任务B侧"))
		{
			taskInfo.m_nPeriodType = -1;
			taskInfo.m_sScheduledTime = QDateTime::currentDateTime().toString("yyyy/MM/dd hh:mm:ss");
		}
		taskInfo.m_sGuid = m_sGuid;
		taskInfo.m_sTaskName = ui.lineEditTaskName->text();
		taskInfo.m_sTemplateFixedID = m_mapTemplate.key(ui.comboBoxTemplate->currentText());

		//wxy++20240430
		taskInfo.m_nContIrTemp = ui.checkBoxContIrTemp->isChecked() ? 1 : 0;//是否开启连续红外测温(0:关闭 1:打开)
		taskInfo.m_nContSoundDet = ui.checkBoxContSoundDet->isChecked() ? 1 : 0;//是否开启连续噪声检测(0:关闭 1:打开)

		QString sErrMsg = "";
		updateTaskInfo(taskInfo,sErrMsg);
		SExecLog sLog;
		sLog.m_nAlarmLevel = enWorkInfo;
		sLog.m_sDescriptionl = ui.lineEditTaskName->text();
		/*sLog.m_sDescriptionl = A2T("%1修改了%2名为%3的任务，其中模版名:%4,任务类型:%5,执行周期:%6")
			.arg(CPublicData::instance().m_userInfo.sUserName)
			.arg(CPublicData::instance().m_mapIFScene[m_sGuid]->m_sSceneName.get())
			.arg(ui.lineEditTaskName->text())
			.arg(ui.comboBoxTemplate->currentText())
			.arg(ui.comboBoxTaskType->currentText())
			.arg(ui.comboBoxPeriod->currentText());*/
		sLog.m_sDescriptionl = A2T("任务");
		CLogOper::instance().addMsg(sLog, m_sGuid);
	}
	QMessageBox::information(this, A2T("提示"), A2T("操作成功!"), QMessageBox::Ok, QMessageBox::No);
	this->close();
}

void CRobTaskOper::slotPushButtonCancleClicked()
{
	this->close();
}

void CRobTaskOper::slotComboxTaskTypeCurrentTextChanged()
{
	if (ui.comboBoxTaskType->currentText() == A2T("周期性任务"))
	{
		ui.lineEditTaskName->setText(A2T("周期性任务%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd")));
		ui.labelTime->show();
		ui.dateTimeEdit->show();
		ui.labelPeriod->show();
		ui.comboBoxPeriod->show();
		ui.dateTimeEdit->hide();
	}
	else if (ui.comboBoxTaskType->currentText() == A2T("定时性任务"))
	{
		ui.lineEditTaskName->setText(A2T("定时性任务%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd")));
		ui.labelTime->show();
		ui.dateTimeEdit->show();
		ui.labelPeriod->hide();
		ui.comboBoxPeriod->hide();
		ui.dateTimeEdit->setDisplayFormat("hh:mm:ss");
	}
	else if (ui.comboBoxTaskType->currentText() == A2T("立即执行任务"))
	{
		ui.lineEditTaskName->setText(A2T("立即执行任务%1").arg(QDateTime::currentDateTime().toString("yyyyMMdd")));
		ui.labelTime->hide();
		ui.dateTimeEdit->hide();
		ui.labelPeriod->hide();
		ui.comboBoxPeriod->hide();
		ui.dateTimeEdit->setDisplayFormat("yyyy/MM/dd hh:mm:ss");
	}
}
